﻿using DL.Animation;
using DL.Character.Core.Base.Movement;
using DL.Characters.Player.FSM.StateMachine;
using DL.Tools;
using UnityEngine;
using UnityEngine.InputSystem;

namespace DL.Characters.Player.FSM.State
{
    public class PlayerLocomotionState : PlayerOnGroundState
    {
        protected MovementActionConfigSO m_MovementActionConfigSo;

        public PlayerLocomotionState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(
            playerStateMachine, property)
        {
            m_MovementActionConfigSo = StateMachine.Player.CharacterSO.MovementActionConfigSO;
        }

        public override void Enter()
        {
            base.Enter();
            Animator.CrossFade("MotionState", ReusableData.AniTransitionTime);
            ReusableData.CanRotation = true;
            if (!ReusableData.Sprinting)
            {
                ReusableData.InputMult =
                    ReusableData.Running ? m_MovementActionConfigSo.RunSpeed : m_MovementActionConfigSo.WalkSpeed;
                ReusableData.RotationTime = 0.2f;
            }
        }

        public override void Exit()
        {
            base.Exit();
            NewTimerManager.Inst.CalcelTimer(m_GameTimer);
        }

        public override void FixedUpdate()
        {
            base.FixedUpdate();
           
        }

        protected override void AddInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.canceled += EnterlocomotionStopStart;
            StateMachine.InputSystem._playerInputActions.LeftControl.started += OnChangeMovementModeStarted;
            StateMachine.InputSystem._playerInputActions.LeftShift.started += OnEnterDodgeState;

            StateMachine.InputSystem._playerInputActions.Movement.started += CancellocomotionStart;

            StateMachine.InputSystem._playerInputActions.Space.started += OnEnterJumpState;
        }

        protected override void RemoveInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.canceled -= EnterlocomotionStopStart;
            StateMachine.InputSystem._playerInputActions.LeftControl.started -= OnChangeMovementModeStarted;
            StateMachine.InputSystem._playerInputActions.LeftShift.started -= OnEnterDodgeState;

            StateMachine.InputSystem._playerInputActions.Movement.started -= CancellocomotionStart;

            StateMachine.InputSystem._playerInputActions.Space.started -= OnEnterJumpState;
        }

        private void OnEnterJumpState(InputAction.CallbackContext context)
        {
            if (CanJump())
            {
                StateMachine.ChangeState<PlayerJumpStartState>();
            }
        }

        private GameTimer m_GameTimer;

        protected void EnterlocomotionStopStart(InputAction.CallbackContext context)
        {
            m_GameTimer = NewTimerManager.Inst.GetTimer(.15f, EnterIdleAni);
        }

        protected virtual void EnterIdleAni()
        {
            if (CanIdleAni())
            {
                //控制一下
                if (ReusableData.Sprinting)
                {
                    Animator.CrossFade("Sprint_Stop", ReusableData.AniTransitionTime);
                    StateMachine.ChangeState<PlayerLocomotionState>();
                }
                else if (ReusableData.Running)
                {
                    Animator.CrossFade("Run_F_Stop", ReusableData.AniTransitionTime);
                }
                else
                {
                    Animator.CrossFade("Walk_Stop", ReusableData.AniTransitionTime);
                }
            }
        }

        protected void CancellocomotionStart(InputAction.CallbackContext context)
        {
            NewTimerManager.Inst.CalcelTimer(m_GameTimer);
        }

        private void OnChangeMovementModeStarted(InputAction.CallbackContext context)
        {
            if (ReusableData.Sprinting) return;
            ReusableData.Running = !ReusableData.Running;
            ReusableData.InputMult =
                ReusableData.Running ? m_MovementActionConfigSo.RunSpeed : m_MovementActionConfigSo.WalkSpeed;
        }


        protected void OnEnterDodgeState(InputAction.CallbackContext context)
        {
            if (CanDodge())
            {
                StateMachine.ChangeState<PlayerDodgeState>();
            }
        }
        
        //********************转换条件检查*************************
        protected bool CanIdleAni()
        {
            if (MoveController.CurrentMovementVelocity.Value > .5f && ReusableData.InputDirection == Vector2.zero &&
                StateMachine.AnimatorStateInfo.IsTag(AnimatorTag.Motion))
            {
                return true;
            }

            return false;
        }

        protected bool CanDodge()
        {
            if (!ReusableData.IsDodge && MoveController.CharacterIsOnGround)
            {
                return true;
            }
            return false;
        }

        protected bool CanJump()
        {
            if (MoveController.CharacterIsOnGround && !StateMachine.AnimatorIsInTransition)
            {
                return true;
            }
            return false;
        }
    }
}